//index.js
// 用户，商家，菜单，订单，子订单，支付，搜索，评价
var util=require('../../utils/util.js');
var foodcart=require('../../utils/foodcart.js')
var app = getApp()
Page({
  data: {
    
    // index
    userInfo: {},
    

    // buycar
    totalMoney: 0,
    buycar_num:0,
    foodlist:[],
    cartlist:[],
    
    // order
    orderOk: false,
    // me
    img: '',
    empty:true,
    imghost:''
  },
  onLoad: function () { 
    this.setData({
      imghost: wx.getStorageSync('imghost')
    })
    
  },
  onShow:function(){
    var ret = foodcart.getFoodlist();
    this.setFoodListData(ret); 
  },

  toFoodDetail: function () {
    wx.navigateTo({
      url: '../detail/detail',
    })
  },

  
  close: function () {
    this.setData({
      block: false
    })
  },
  resetNum: function (e) {
    var type = e.currentTarget.dataset.id;
    this.setData({
      foodtype: type
    })
  },
  submit: function () {
    var i = this.data.bindId;
    var arr3 = this.data.arr3;
    arr3[i].num = parseInt(arr3[i].num) + 1
    this.setData({
      block: false,
      arr3: arr3
    })
  },
  
  toSubmit: function () {
    var foodlist = this.data.foodlist;
    var selected=false;
    for (var i = 0; i < foodlist.length;i++){
      if (foodlist[i].selected){
        selected=true;
        continue;
      }
    }
    if (!selected){
      util.showinfo("请先选中菜品"); return ;
    }
    wx.navigateTo({
      url: '../submitOrder/submitOrder',
    })
  },
  
  
  addcart:function(e){
    var id = e.currentTarget.dataset.id;
    this.updatecart(id, 1);
  },
  reducecart: function (e){
    var id = e.currentTarget.dataset.id;
    this.updatecart(id,-1);
  },
  updatecart:function(id,num){
    var ret = foodcart.addFoodCart(id, num);
    this.setFoodListData(ret);  
  },

  delcart:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var num = e.currentTarget.dataset.num;
    wx.showModal({
      title: '提示',
      content: '确定删除吗',
      success: function (res) {
        that.updatecart(id, 0 - num);
      }
    })
  },
  selectedFoodCart:function(e){
    var id = e.currentTarget.dataset.id;
    var ret = foodcart.selectedFoodCart(id);
    this.setFoodListData(ret);
  },
  setFoodListData:function(ret){
    this.setData({
      foodlist: ret.foodlist,
      totalMoney: ret.buycart.buyamount,
      buycar_num: ret.buycart.buynum,
      cartlist:foodcart.getCartList(),
    })
  },
  goToGetFood:function(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  }
})
